package com.l.web.service.login;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.l.qiao.Global;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class CaptchaService extends ActionSupport {

    private static final Logger LOGGER = Logger.getLogger(CaptchaService.class);
    private int imageWidth = 70;
    private int imageHeight = 20;
    private int codeNumber = 4;
    private int backGround = 20;
    private String codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private Random random = new Random(new Date().getTime());

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public int getCodeNumber() {
        return codeNumber;
    }

    public void setCodeNumber(int codeNumber) {
        this.codeNumber = codeNumber;
    }

    public int getBackGround() {
        return backGround;
    }

    public void setBackGround(int backGround) {
        this.backGround = backGround;
    }

    private char getCode() {
        int index = random.nextInt(codes.length());
        return codes.charAt(index);
    }

    public String execute() {
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/jpeg");
        BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_BGR);
        Graphics2D g = bufferedImage.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, imageWidth, imageHeight);
        for (int i = 0; i < backGround; i++) {
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            g.setColor(new Color(red, green, blue));
            int startX = random.nextInt(imageWidth);
            int startY = random.nextInt(imageHeight);
            if (random.nextInt(5) % 3 == 0) {
                int r = random.nextInt(10);
                int startAngle = random.nextInt(360);
                int arcAngle = random.nextInt(360);
                g.drawArc(startX, startY, r, r, startAngle, arcAngle);
            } else {
                int changeX = random.nextInt() % 6;
                int changeY = random.nextInt() % 6;
                g.drawLine(startX, startY, startX + changeX, startY + changeY);
            }
        }
        StringBuffer ValidateCode = new StringBuffer();
        g.setFont(new Font("Tahoma", Font.BOLD, imageHeight - 3));
        for (int i = 0; i < codeNumber; i++) {
            int red = random.nextInt(100) + 50;
            int green = random.nextInt(100) + 50;
            int blue = random.nextInt(100) + 50;
            g.setColor(new Color(red, green, blue));
            char code = getCode();
            ValidateCode.append(code);
            g.drawString(code + "", imageWidth / codeNumber * i + 4, 16);
        }
        request.getSession().setAttribute(Global.SESSION_CAPTCHA, ValidateCode.toString());
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            ImageIO.write(bufferedImage, "jpeg", sos);
            bufferedImage.flush();
            bufferedImage = null;
            sos.close();
        } catch (IOException e) {
            LOGGER.error("图片输出异常：" + e);
        }
        return NONE;
    }
}
